package com.tmzn.rewrite.util;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;

/**
 * PHP相关的工具类
 */
public class PhpUtil {
    public static String payLogTable = "ims_core_paylog_";

    public static String refundLogTable = "ims_core_refundlog_";

    public static String billTable = "ims_mc_bill_";


    public static void main(String[] args) {
        System.out.println(time());
    }

    public static Integer getTimeSeconds(){
            return time();
    }
    /**
     * 根据秒获取支付记录的分表
     * @param seconds
     * @return
     */
    public static String getPayLogTable(int seconds){
        return payLogTable+getTableSuffix(seconds);
    }


    /**
     * 根据秒获取退款记录的分表
     * @param seconds
     * @return
     */
    public static String getRefundLogTable(int seconds){
        return refundLogTable+getTableSuffix(seconds);
    }


    /**
     * 根据秒获取退款记录的分表
     * @return
     */
    public static String getRefundLogTable(){
        int seconds =  time();
        return refundLogTable+getTableSuffix(seconds);
    }


    /**
     * 根据秒获取汇报记录的分表
     * @return
     */
    public static String getRefundLogTableLastMonth(){
        int seconds = time();
        return refundLogTable+getTableSuffixLastMonth(seconds);
    }


    /**
     * 根据秒获取汇报记录的分表
     * @param seconds
     * @return
     */
    public static String getRefundLogTableLastMonth(int seconds){
        return refundLogTable+getTableSuffixLastMonth(seconds);
    }

    /**
     * 获取分账记录表
     * @return
     */
    public static String getBillTable(){
        int seconds =  time();
        return billTable+getTableSuffix(seconds);
    }


    /**
     * 根据秒获取分账记录表
     * @param seconds
     * @return
     */
    public static String getBillTable(int seconds){
        return billTable+getTableSuffixLastMonth(seconds);
    }


    /**
     * 根据毫秒数判断月份
     * @param seconds
     * @return
     */
    public static String getTableSuffix(int seconds){
        Instant instant = Instant.ofEpochSecond(seconds);
        LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        int year = dateTime.getYear();
        Month monthObj = dateTime.getMonth();
        String month = monthObj.getValue()+"";
        if(month.length() == 1){
            month = "0"+month;
        }
        return year+month;
    }

    /**
     * 根据毫秒数判断上个月
     * @param seconds
     * @return
     */
    public static String getTableSuffixLastMonth(int seconds){
        Instant instant = Instant.ofEpochSecond(seconds);
        LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        dateTime = dateTime.minusMonths(1);
        int year = dateTime.getYear();
        Month monthObj = dateTime.getMonth();
        String month = monthObj.getValue()+"";
        if(month.length() == 1){
            month = "0"+month;
        }
        return year+month;
    }

    public static String getBillTableLast() {
        int seconds =  time();
        return billTable+getTableSuffixLastMonth(seconds);
    }

    public static Integer time() {
        Long value = System.currentTimeMillis();
        value = value/1000;
        int seconds =  value.intValue();
        return seconds;
    }
}
